
package Interface.Game;

import Interface.JPanelAccueil;
import Jastermind.Appli;
import Jastermind.Game;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;


public class JDialogEndGame extends JDialog{
    public JDialogEndGame(boolean gameWon,Game game){
        super();
        this.setTitle("Fin de la partie");
        this.setModal(true);
        this.setSize(350,140);
        this.setLayout(null);
        this.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);

        this.setResizable(false);
        this.getContentPane().setBackground(Color.WHITE);
        Dimension screenDimension = Toolkit.getDefaultToolkit().getScreenSize();
        //centrer la frame
        this.setLocation((int) (screenDimension.getWidth() - this.getWidth()) / 2, (int) (screenDimension.getHeight() - this.getHeight()) / 2);

        JLabel jblTitre = new JLabel();
        jblTitre.setBounds(90,0, 200, 20);
        if(gameWon==true){
            game.getPlayer().setGameWon(game.getPlayer().getGameWon()+1);
            jblTitre.setText("Félications! Vous avez gagné");
        }
        else{
            game.getPlayer().setGameLost(game.getPlayer().getGameLost()+1);
           jblTitre.setText("Dommage! Vous avez perdu");
        }
        this.add(jblTitre);

        for(int iColor=game.getCombinaison().size()-1;iColor>=0;iColor--){
          JLabel jblColor=new JLabel();
          jblColor.setOpaque(true);
          jblColor.setBounds(210-iColor*30,30, 20, 20);
          jblColor.setBackground(game.getCombinaison().get(iColor));
          this.add(jblColor);
        }
        JButton jbtReplay=new JButton("Rejouer");
        jbtReplay.setBounds(20,60,150,30);
        jbtReplay.addActionListener(new ActionJDEndGame());
        this.add(jbtReplay);

        JButton jbtQuitter=new JButton("Quitter");
        jbtQuitter.setBounds(180,60,150, 30);
        jbtQuitter.addActionListener(new ActionJDEndGame());
        this.add(jbtQuitter);

        this.setVisible(true);
    }
     private class ActionJDEndGame implements ActionListener{

        public void actionPerformed(ActionEvent e) {
            Appli.getJfMain().resetJFMainSettings();
            if(e.getActionCommand().equals("Quitter")){
                Appli.getJfMain().setJPContent(new JPanelAccueil());
            }
             if(e.getActionCommand().equals("Rejouer")){
                 Appli.getJfMain().setJPContent(new JPanelGameSettings());
             }
             dispose();
        }

    }
}

